package org.example.lambda;

import org.example.entity.Person;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @author why
 * @date 2022/11/1221:11
 */
public class Unit1ExerciseJava8 {


    public static void main(String[] args) {


        List<Person> personList = Arrays.asList(
                new Person("Cheng1","fen",23),
                new Person("Cheng2","fen",23),
                new Person("Xiao","Can",23),
                new Person("Chang3","yang",23),
                new Person("Clin","Yang",23),
                new Person("Lin","Cang",23)
        );

        // Step 1 : Sort list by last name
        Collections.sort(personList, Comparator.comparing(Person::getLastName));


        // Step 2: Create a method that print all element in the list
        System.out.println("printAllElement");

        printAllElementBeginningWithCondition(personList,p->true);

        // Step 3: Create a method that print all element that have last name beginning with C

        System.out.println("printAllElementLastNameBeginningWithC");
        printAllElementBeginningWithCondition(personList,p->p.getLastName().startsWith("C"));


        // Step 4: Create a method that print all element that have first name beginning with C
        System.out.println("printAllElementFirstNameBeginningWithC");
        printAllElementBeginningWithCondition(personList,p->p.getFirstName().startsWith("C"));

        // use java.util.predicate .the same as printAllElementBeginningWithCondition
        printAllElementBeginningWithPredicate(personList,p->p.getFirstName().startsWith("C"));


        System.out.println("printAllElementBeginningWithPredicateAndConsumer");
        printAllElementBeginningWithPredicateAndConsumer(personList,person -> person.getFirstName().startsWith("C"), person -> System.out.println(person.getFirstName()));


        printAllElementBeginningWithPredicateAndConsumer(personList,person -> person.getFirstName().startsWith("C"), person -> System.out.println(person.getLastName()));


    }


    private static void printAllElementBeginningWithCondition(List<Person> personList,Condition condition) {
        for(Person person:personList){
            if (condition.test(person)){
                System.out.println(person);
            }
        }

    }

    /**
     * java doc test 。the same as the Condition which we created
     * @param personList
     * @param predicate
     */
    private static void printAllElementBeginningWithPredicate(List<Person> personList, Predicate<Person> predicate) {
        for(Person person:personList){
            if (predicate.test(person)){
                System.out.println(person);
            }
        }

    }
    private static void printAllElementBeginningWithPredicateAndConsumer(List<Person> personList, Predicate<Person> predicate, Consumer<Person> consumer) {
        for(Person person:personList){
            if (predicate.test(person)){
                consumer.accept(person);
            }
        }

    }



}